// 
//  GFWInterceptorLibrary.mm
//  GFWInterceptor
//  
//  Created by Chen Zhang on 2010-06-24.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
// 


#import "GFWInterceptorLibrary.h"

NSString * const twittersearch = @"http://search.twitter.com";
NSString * const twitteroauth = @"http://api.twitter.com";
NSString * const twitterother = @"http://api.twitter.com/1";
NSString * const twitterother2 = @"http://twitter.com";
NSString * const twittersearchs = @"https://search.twitter.com";
NSString * const twitteroauths = @"https://api.twitter.com";
NSString * const twitterothers = @"https://api.twitter.com/1";    
NSString * const twitterother2s = @"https://twitter.com";    


static IMP original_NSURL_initWithString_;
static IMP original_NSURL_initWithString_relativeToURL_;
static IMP original_NSURL_absoluteString_;


id replaced_NSURL_initWithString_(NSURL *self, SEL _cmd, NSString *URLString){
    #ifdef DEBUG
    NSLog(@"Haha! We have intercepted this url: %@",URLString);
    #endif

    NSString *newstr = URLString;
    newstr = replaceString(newstr,twittersearchs,searchapi);
    newstr = replaceString(newstr,twittersearch,searchapi);
    newstr = replaceString(newstr,twitterothers,otherapi);
    newstr = replaceString(newstr,twitterother,otherapi);
    newstr = replaceString(newstr,twitteroauths,oauthapi);
    newstr = replaceString(newstr,twitteroauth,oauthapi);
    newstr = replaceString(newstr,twitterother2s,otherapi);
    newstr = replaceString(newstr,twitterother2,otherapi);
    
    URLString = newstr;
    #ifdef DEBUG
    NSLog(@"======, Changed URL %@",URLString);
    #endif
    return original_NSURL_initWithString_(self, _cmd,URLString);
}


id replaced_NSURL_initWithString_relativeToURL_(NSURL *self, SEL _cmd, NSString *URLString, NSURL* baseURL){

    #ifdef DEBUG
    NSLog(@"Haha! We have intercepted this url: %@",URLString);
    #endif
    
    NSString *newstr = URLString;
    newstr = replaceString(newstr,twittersearchs,searchapi);
    newstr = replaceString(newstr,twittersearch,searchapi);
    newstr = replaceString(newstr,twitterothers,otherapi);
    newstr = replaceString(newstr,twitterother,otherapi);
    newstr = replaceString(newstr,twitteroauths,oauthapi);
    newstr = replaceString(newstr,twitteroauth,oauthapi);
    newstr = replaceString(newstr,twitterother2s,otherapi);
    newstr = replaceString(newstr,twitterother2,otherapi);
    
    URLString = newstr;
    #ifdef DEBUG
    NSLog(@"======, Changed URL %@",URLString);
    #endif
    
    return original_NSURL_initWithString_relativeToURL_(self, _cmd,URLString,baseURL);
}

id replaced_NSURL_absoluteString_(NSURL *self, SEL _cmd){
    NSString *original = original_NSURL_absoluteString_(self,_cmd);
    #ifdef DEBUG
    NSLog(@"***I caught you use absolute string %@",original);
    #endif
    
    NSString *newstr = original;
    NSString *changed = nil;
    
    if([newstr rangeOfString:@"search"].location != NSNotFound){
        changed  = replaceString(newstr,searchapi,twitterothers);
        if(changed != newstr) return changed;
    }
    
    if([newstr rangeOfString:@"oauth"].location != NSNotFound){    
        changed = replaceString(newstr,oauthapi,twitteroauths);
        if(changed != newstr) return changed;
    }
    newstr = replaceString(newstr,otherapi,twitterothers);
        
    return newstr;
}

NSString *replaceString(NSString *original, NSString *before, NSString *after){
    int location = [original rangeOfString:before].location;
    if(location != NSNotFound && location == 0){
        NSRange range;
        range.location = 0;
        range.length = [before length];
        NSString *newstr = [original stringByReplacingOccurrencesOfString:before withString:after options:0 range:range];
        return newstr;
    }
    return original;
}


extern "C" void GFWInterceptorInitialize() {
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];    
    NSString *identifier = [[NSBundle mainBundle] bundleIdentifier];

	if ([identifier rangeOfString:@".apple."].location == NSNotFound) {
	    #ifdef DEBUG
        NSLog(@"init GFWInterceptor, this should be cool");
        #endif 

        NSMutableDictionary *prefs = [[NSMutableDictionary alloc] initWithContentsOfFile:@"/var/mobile/Library/Preferences/com.overboming.gfwinterceptor.plist"] ?: [[NSMutableDictionary alloc] init];
        BOOL enabled = [prefs objectForKey:@"isenabled"] ? [[prefs objectForKey:@"isenabled"] boolValue] : YES;
        
        if (!enabled){
            [pool release];
            return;
        }
        
        searchapi = [prefs objectForKey:@"searchproxy"] ? [prefs objectForKey:@"searchproxy"] : @"http://waytosing.appspot.com/twitter";
        otherapi = [prefs objectForKey:@"apiproxy"] ? [prefs objectForKey:@"apiproxy"] : @"http://waytosing.appspot.com/twitter";
        oauthapi = otherapi;
        
        Class _$NSURL = [NSURL class];

        MSHookMessageEx(_$NSURL,@selector(initWithString:),(IMP)replaced_NSURL_initWithString_,(IMP *)&original_NSURL_initWithString_);
        MSHookMessageEx(_$NSURL,@selector(initWithString:relativeToURL:),(IMP)replaced_NSURL_initWithString_relativeToURL_, (IMP *)&original_NSURL_initWithString_relativeToURL_);
        MSHookMessageEx(_$NSURL,@selector(absoluteString),(IMP)replaced_NSURL_absoluteString_,(IMP *)&original_NSURL_absoluteString_);
    
	}

    [pool release];
}
